<template>
  <div class="user-info-head" @click="open()">
    <el-avatar v-if="sourceValue" :src="sourceValue" alt="avatar" class="img-circle img-lg" />
    <el-avatar v-else :src="avatar" alt="avatar" class="img-circle img-lg" />
    <e-button v-if="showBtn" :class="`${prefixCls}-upload-btn`" @click="open()">
      {{ btnText ? btnText : t('cropper.selectImage') }}
    </e-button>
    <CopperModal
      ref="cropperModelRef"
      :srcValue="sourceValue"
      @upload-success="handleUploadSuccess"
    />
  </div>
</template>
<script lang="ts" setup>
  import { ref, watch, watchEffect } from 'vue'
  import { useDesign } from '@/hooks/web/useDesign'
  import { useMessage } from '@/hooks/web/useMessage'
  import { useI18n } from '@/hooks/web/useI18n'
  import { propTypes } from '@/utils/propTypes'
  import CopperModal from './CopperModal.vue'
  import avatar from '@/assets/imgs/avatar.gif'

  defineOptions({ name: 'CropperAvatar' })

  const props = defineProps({
    width: propTypes.string.def('200px'),
    value: propTypes.string.def(''),
    showBtn: propTypes.bool.def(true),
    btnText: propTypes.string.def('')
  })

  const emit = defineEmits<{
    'update:value': [value: string]
    'change': [data: { source: string; data: any; filename: string }]
  }>()
  
  const sourceValue = ref(props.value)
  const { getPrefixCls } = useDesign()
  const prefixCls = getPrefixCls('cropper-avatar')
  const message = useMessage()
  const { t } = useI18n()

  const cropperModelRef = ref()

  watchEffect(() => {
    sourceValue.value = props.value
  })

  watch(
    () => sourceValue.value,
    (v: string) => {
      emit('update:value', v)
    }
  )

  interface UploadSuccessData {
    source: string
    data: any
    filename: string
  }

  function handleUploadSuccess({ source, data, filename }: UploadSuccessData) {
    sourceValue.value = source
    emit('change', { source, data, filename })
    message.success(t('cropper.uploadSuccess'))
  }

  function open() {
    cropperModelRef.value?.openModal()
  }

  function close() {
    cropperModelRef.value?.closeModal()
  }

  defineExpose({
    open,
    close
  })
</script>
<style lang="scss" scoped>
  
  $prefix-cls: #{$namespace}--cropper-avatar;

  .#{$prefix-cls} {
    display: inline-block;
    text-align: center;

    &-image-wrapper {
      overflow: hidden;
      cursor: pointer;
      border: 1px solid;
      border-radius: 50%;

      img {
        width: 100%;
      }
    }

    &-image-mask {
      position: absolute;
      width: inherit;
      height: inherit;
      cursor: pointer;
      background: rgb(0 0 0 / 40%);
      border: inherit;
      border-radius: inherit;
      opacity: 0;
      transition: opacity 0.4s;

      ::v-deep(svg) {
        margin: auto;
      }
    }

    &-image-mask:hover {
      opacity: 40;
    }

    &-upload-btn {
      margin: 10px auto;
    }
  }

  .user-info-head {
    position: relative;
    display: inline-block;
  }

  .img-circle {
    border-radius: 50%;
  }

  .img-lg {
    width: 120px;
    height: 120px;
  }

  .user-info-head:hover::after {
    position: absolute;
    inset: 0;
    left: 0;
    top: 0;
    font-size: 24px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-style: normal;
    line-height: 110px;
    height: 120px;
    width: 120px;
    color: #eee;
    cursor: pointer;
    background: rgb(0 0 0 / 50%);
    border-radius: 50%;
    content: '+';
    font-size: 40px;
    text-align: center;
  }
</style>
